//
//  MBNavigationController.swift
//  MicroBlog
//
//  Created by ChenJunWen on 2017/11/28.
//  Copyright © 2017年 CJW. All rights reserved.
//

import UIKit

class MBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.isHidden = true
    }
    
    // 重写push方法,所有的push操作都会调用此方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 判断不是根控制器才需要隐藏底部tabbar
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? MBBaseViewController {
                var title: String = "返回"
                if childViewControllers.count == 1 { // 只有一个子控制器的时候左边返回键显示标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                vc.customNavItem.leftBarButtonItem = UIBarButtonItem(title: title, fontsize: 16, target: self, action: #selector(popToParent), isBack: true)
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    // 回到上一个页面
    func popToParent() -> () {
        popViewController(animated: true)
    }
    
}
